package com.example.peibei.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.dingtao.common.bean.GetOrder;
import com.dingtao.common.bean.Order;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDFragment;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.SPUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.service.presenter.GetCommentPresenter;
import com.example.peibei.service.presenter.GetOrderPresenter;
import com.example.peibei.ui.activity.EvaluateActivity;
import com.example.peibei.ui.activity.MyGetOrderDetailActivity;
import com.example.peibei.ui.adapter.CommentListAdapter;
import com.example.peibei.ui.adapter.GetOrderAdapter;
import com.example.peibei.ui.weight.EmptyRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MyCommentListFragment extends WDFragment {
    @BindView(R.id.rv_get_order)
    EmptyRecyclerView rv_get_order;
    @BindView(R.id.empty_view)
    LinearLayout empty_view;

    private int status;
    List<Order.RecordsDTO> list = new ArrayList<>();
    private CommentListAdapter getOrderAdapter;
    private GetCommentPresenter getOrderPresenter;
    private SPUtils spUtils;
    private String token;

    public MyCommentListFragment(int status) {
        this.status = status;
    }

    @Override
    public String getPageName() {
        return "订单fragment";
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_my_get_order;
    }

    @Override
    protected void initView() {
        spUtils = new SPUtils(getActivity(), SpConstant.USER_TOKEN);
        token = spUtils.getString(SpConstant.USER_TOKEN);
        rv_get_order.setLayoutManager(new LinearLayoutManager(getActivity()));
        getOrderAdapter = new CommentListAdapter(list, status);
        rv_get_order.setAdapter(getOrderAdapter);
        rv_get_order.setEmptyView(empty_view);
        getOrderPresenter = new GetCommentPresenter(new OrderCall());
        getOrderAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                List<Order.RecordsDTO> data = (List<Order.RecordsDTO>) adapter.getData();
                Order.RecordsDTO order = data.get(position);
                String consumeOrderId = order.getOrderId();
                Bundle bundle = new Bundle();
                bundle.putString("consumeOrderId", consumeOrderId);
                bundle.putString("userId", order.getMember().getMid());
                bundle.putString("nickName", order.getMember().getNickName());
                if (status == 0) {
                    bundle.putInt("status", 3);
                } else {
                    bundle.putInt("status", 4);
                }
                intent(MyGetOrderDetailActivity.class, bundle);
            }
        });
        getOrderAdapter.addChildClickViewIds(R.id.bt_to_comment);
        getOrderAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                List<Order.RecordsDTO> data = (List<Order.RecordsDTO>) adapter.getData();
                Order.RecordsDTO order = data.get(position);
                String consumeOrderId = order.getOrderId();
                Bundle bundle = new Bundle();
                bundle.putString("token", token);
                bundle.putString("consumeOrderId", consumeOrderId + "");
                intent(EvaluateActivity.class, bundle);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        getOrderPresenter.reqeust(true, "Bearer " + token, status);
    }

    class OrderCall implements DataCall<Order> {

        @Override
        public void success(Order data, Object... args) {
//            EventBus.getDefault().post(RefreshResult.getInstance(true, true));
            //添加列表并刷新
            if (getOrderPresenter.getPage() == 1) {
                list.clear();
            }
            getOrderAdapter.setNewInstance(data.getRecords());
            getOrderAdapter.notifyDataSetChanged();
        }

        @Override
        public void fail(ApiException e, Object... args) {
//            EventBus.getDefault().post(RefreshResult.getInstance(true, true));
        }
    }
}
